//
//  MingxiViewController.swift
//  联付商圈
//
//  Created by zhangqi on 2017/7/20.
//  Copyright © 2017年 lianfu. All rights reserved.
//

import UIKit
import Alamofire

class MingxiViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var show = 1
    //var listArray = ["全部","收入","支出"]
    var list = [[String:Any]]()
    
    @IBOutlet weak var listTableView: UITableView!

    @IBOutlet weak var dataTableView: UITableView!
    
    @IBOutlet weak var backgroundImage: UIImageView!
    
    
    @IBOutlet weak var midBtn: UIButton!
    
    
    @IBAction func midBtn(_ sender: UIButton) {
        if show == 2 {
            hid()
            midBtn.setImage(UIImage(named:"收下"), for:.normal)
        } else if show == 1 {
            UITableView.animate(withDuration: 0.2, animations: {
                self.listTableView.frame = CGRect(x: 0, y: 64, width: mainScreenW, height:55*3)
                self.backgroundImage.alpha = 0.6
            })
            midBtn.setImage(UIImage(named:"收上"), for:.normal)
           show = 2
        }
    }
    


    override func viewDidLoad() {
        super.viewDidLoad()
        midBtn.isHidden = true
        backgroundImage.isHidden = true
        listTableView.isHidden = true
        backgroundImage.isHidden = true
        shouzhimingxi()
        listTableView.tag = 101
        dataTableView.tag = 102
        listTableView.frame = CGRect(x: 0, y: -55*7+64, width: mainScreenW, height:55*7)
        touch()
        dataTableView.tableFooterView = UIView(frame:CGRect.zero)
    }

    override func viewWillAppear(_ animated: Bool) {
        //self.navigationController?.navigationBar.isHidden = true
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    
    func shouzhimingxi(){
        let x = UserDefaults.standard.object(forKey: "userInfo") as! NSDictionary
        let y = x["name"]!
        let yhid = ["name":"\(y)"]
        let urlString = "https://www.beidikeji.com/life/public/jiekou/szmx?"
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45
        manager.request(urlString,method:.post,parameters: yhid).responseJSON {Request in
            switch Request.result {
            case .success:
                let json = JSON(data: Request.data!)
//                print(json)
//                print(json)
                let x = json.arrayObject as! [[String:Any]]
                self.list = x
                self.dataTableView.reloadData()
                //self.noticeInfo("编辑成功", autoClear: true, autoClearTime: 1)
            case .failure:
                self.noticeInfo("未知错误", autoClear: true, autoClearTime: 1)
            }
        }
    }

    
    
    
    
    func touch() {
        let tap = UITapGestureRecognizer(target : self,action : #selector(hid))
        tap.numberOfTapsRequired = 1
        backgroundImage.isUserInteractionEnabled = true
        backgroundImage.addGestureRecognizer(tap)
        
    }
    func hid() {
        UITableView.animate(withDuration: 0.2, animations: {
            self.backgroundImage.alpha = 0
            self.listTableView.frame = CGRect(x: 0, y: -55*7+64, width: mainScreenW, height:55*3)
        })
        show = 1
    }

    
    
    
    
    
    
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 101 {
            return 0//listArray.count
        } else if tableView.tag == 102 {
            return list.count
        } else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView.tag == 101 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableViewCell", for: indexPath) as! ListTableViewCell
            cell.listLabel.text = ""//listArray[indexPath.row]
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DataTableViewCell", for: indexPath) as! DataTableViewCell
            let x = list.count
            if x != 0 {
                cell.timeLab.text = "下单时间:" + "\(list[indexPath.row]["created_at"]!)"
                cell.totalLab.text = "¥" + "\(list[indexPath.row]["allprice"]!)"
                cell.numLab.text = "订单号:" + "\(list[indexPath.row]["ddh"]!)"
                return cell
            } else {
                return cell
            }
            
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dataTableView.deselectRow(at: indexPath, animated: false)
        if tableView.tag == 101 {
//            switch indexPath.row {
//            case 0:
//                hid()
//            case 1:
//                hid()
//            case 2:
//                hid()
//            default:
//                break
//            }
        } else if tableView.tag == 102 {
            ddh = "\(list[indexPath.row]["ddh"]!)"
            let sb = UIStoryboard(name: "Main", bundle:nil)
            let secondViewController = sb.instantiateViewController(withIdentifier: "dingdanxiangqing") as! UINavigationController
            self.present(secondViewController, animated: true, completion: nil)
        }
        
    }

    

 

}
